#include "testhttp.h"
#include <qpushbutton.h>
#include <qtextcodec.h>
#include <qjsondocument.h>
#include <qjsonobject.h>

#include "httplib.h"
#include <Windows.h>
#include "nlohmann/json.hpp"

using DefJson = nlohmann::json;


#pragma execution_character_set("utf-8")

namespace {
    constexpr unsigned int g_connect_timeout{10'000'000};
}



testhttp::testhttp(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
    OnInitDialog();
}

testhttp::~testhttp()
{}

void testhttp::OnInitDialog()
{
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("utf-8"));
    auto _btn = new QPushButton(tr("test function"), this);
    Q_ASSERT(_btn);


    _btn->move(200, 200);
    connect(_btn, &QPushButton::clicked, []() {
        httplib::Client _client("http://192.168.5.94:80");
        _client.set_connection_timeout(0, g_connect_timeout);
#if 1
        DefJson _json_obj{DefJson::object()};
        _json_obj["loginName"] = "operation1";
        _json_obj["password"] = "123456";
        _json_obj["machineCode"] = "031B021C-040D-05AF-DB06-5E0700080009";

        auto _body = _json_obj.dump();
        std::string _content_type{"application/json"};
        auto _res = _client.Post("/login", _body, _content_type);
        auto _err = _res.error();
        
        auto _value = QString::fromLocal8Bit( _res->body.c_str());
        DefJson _response = DefJson::parse(_res->body);
        auto _success = _response["success"].template get<bool>();
        auto _code = _response["code"].template get<int>();
        auto _token = _response["data"]["jwtToken"].template get<std::string>();
#else
        httplib::Headers _header{ {"content-type","application/x-www-form-urlencoded"},{"Datagrid-Authorization","eyJhbGciOiJSUzI1NiJ9.eyJkYXRhZ3JpZF91c2VyIjoie1widXNlcklkXCI6MixcImxvZ2luTmFtZVwiOlwib3BlcmF0aW9uMVwiLFwicGFzc3dvcmRcIjpcIjEyMzQ1NlwiLFwibWFjaGluZUNvZGVcIjpcIjAzMUIwMjFDLTA0MEQtMDVBRi1EQjA2LTVFMDcwMDA4MDAwOVwiLFwicmVtb3RlQWRkclwiOlwiMTkyLjE2OC41LjE1XCIsXCJyZW1vdGVIb3N0XCI6XCIxOTIuMTY4LjUuMTVcIixcInNlcnZsZXRQYXRoXCI6bnVsbCxcIm1ldGhvZFwiOm51bGx9IiwianRpIjoiWWpJME16Z3dZVGt0WkRCaU1DMDBZakUxTFdKaU16UXRORFZpTWpsbE1qWXpZekl4IiwiZXhwIjoxNjk4NzIxOTk1fQ.UBUzhdZTtJCJj3YXr3oSsJf6wQtn0dXdLfr2KR0P4-acvq7rT1iG9sueepmZ0lCGmEYdMBo-2-hYLy6C78h6R2N_RPqelfKtjwNnjCx8yr7Y5yfMXhAfH0Gi7qsfwyS2izBsM8-nt9e6h8mds9hffLyEBGSnjc_DTbFSYLf360g"} };
        auto _res = _client.Get("/security/manager/client/queryAll", _header);
        auto _value = QString::fromLocal8Bit(_res->body.c_str());
#endif // 0

        int a = 10;
        });
}
